﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PollSystemDAL
{
    public static class PollSystemDAO
    {       
        public static Question[] GetQuestions()
        {
            using (PollSystemEntities context = new PollSystemEntities())
            {
                return context.Questions.ToArray();
            }
        }

        public static Answer[] GetAnswers()
        {
            using (PollSystemEntities context = new PollSystemEntities())
            {
                return context.Answers.ToArray();
            }
        }

        public static Answer[] GetAnswersByQuestionId(int questionId)
        {
            using (PollSystemEntities context = new PollSystemEntities())
            {
                return context.Answers.Where(a => a.QuestionId == questionId).ToArray();
            }
        }

        public static void VoteForAnswer(int answerId)
        {
            using (PollSystemEntities context = new PollSystemEntities())
            {
                Answer answer = context.Answers.First(a => a.AnswerId == answerId);
                answer.VotesCount++;
                context.SaveChanges();
            }
        }

        public static int GetQuestionIdByAnswerId(int answerId)
        {
            using (PollSystemEntities context = new PollSystemEntities())
            {
                return context.Answers.First(a => a.AnswerId == answerId).QuestionId;
            }
        }
    }
}
